{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4]\n(5,)\nint32\n"
     ]
    }
   ],
   "source": [
    "from numpy import *\n",
    "\n",
    "# 2.1 NumPy数组对象\n",
    "\n",
    "a = arange(5)\n",
    "print(a)\n",
    "print(a.shape)\n",
    "print(a.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1]\n [0 1]]\n(2, 2)\n"
     ]
    }
   ],
   "source": [
    "# 2.2 创建多维数组\n",
    "\n",
    "m = array([arange(2), arange(2)])\n",
    "print(m)\n",
    "print(m.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "# 2.2.1 选取数组元素\n",
    "\n",
    "print(m[1, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "float32\n"
     ]
    }
   ],
   "source": [
    "# 2.2.4 字符编码（整数、无符号整数、单精度浮点数等）\n",
    "\n",
    "b = arange(7, dtype='f')\n",
    "print(b.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 2 4 6]\n[8 7 6 5 4 3 2 1]\n"
     ]
    }
   ],
   "source": [
    "# 2.4 一维数组的索引和切片\n",
    "\n",
    "# 0到7，步长为2\n",
    "a = arange(9)\n",
    "print(a[0:7:2])\n",
    "\n",
    "# 反转数组\n",
    "print(a[9:0:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n  [ 4  5  6  7]\n  [ 8  9 10 11]]\n\n [[12 13 14 15]\n  [16 17 18 19]\n  [20 21 22 23]]]\n(2, 3, 4)\n22\n"
     ]
    }
   ],
   "source": [
    "# 2.5 可以使用省略号(...)来表示遍历剩下的维度。\n",
    "\n",
    "# 改变维度，一维24个元素转成2组 3*4 的矩阵。\n",
    "a = arange(24).reshape(2, 3, 4)\n",
    "print(a)\n",
    "print(a.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 选取数据\n",
    "\n",
    "# 选取一个数据\n",
    "print(a[1, 2, 2])\n",
    "\n",
    "# 选取多个数据\n",
    "print(a[0, :, :])\n",
    "\n",
    "# 省略号代替\n",
    "\n",
    "print(a[0, ...])\n",
    "\n",
    "# 选取前两维也是可以的\n",
    "\n",
    "print(a[0, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n"
     ]
    }
   ],
   "source": [
    "# 2.6 改变数组的维度\n",
    "# (1) ravel 完成展平的操作\n",
    "\n",
    "b = arange(24).reshape(2,3,4)\n",
    "# print(b)\n",
    "print(b.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n"
     ]
    }
   ],
   "source": [
    "# (2) flatten 展平。 flatten函数会请求分配内存来保存结果。而ravel函数只是返回数组的一个视图。\n",
    "\n",
    "print(b.flatten())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n [ 4  5  6  7]\n [ 8  9 10 11]\n [12 13 14 15]\n [16 17 18 19]\n [20 21 22 23]]\n"
     ]
    }
   ],
   "source": [
    "# (3) 用元组设置维度。除了reshape,还可直接用一个正整数元组来设置数组的维度。\n",
    "\n",
    "b.shape = (6,4)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  4  8 12 16 20]\n [ 1  5  9 13 17 21]\n [ 2  6 10 14 18 22]\n [ 3  7 11 15 19 23]]\n"
     ]
    }
   ],
   "source": [
    "# (4) transpose 转置。可以用于多维数组。\n",
    "print(b.transpose())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n[[ 0  1  2  3  4  5  6  7  8  9 10 11]\n [12 13 14 15 16 17 18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "source": [
    "# (4) resize 和 reshape() 函数的功能一样，但resize会直接修改所操作的数组。\n",
    "\n",
    "print(b.resize(2, 12))\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  2  4]\n [ 6  8 10]\n [12 14 16]]\n"
     ]
    }
   ],
   "source": [
    "# 数组的组合  \n",
    "# 乘法计算\n",
    "a = arange(9).reshape(3,3)\n",
    "b = a * 2\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  0  2  4]\n [ 3  4  5  6  8 10]\n [ 6  7  8 12 14 16]]\n==========================================================================================================\n[[ 0  1  2  0  2  4]\n [ 3  4  5  6  8 10]\n [ 6  7  8 12 14 16]]\n"
     ]
    }
   ],
   "source": [
    "# (1) 水平组合。将ndarray对象构成的元组作为参数，传给hstack\n",
    "print(hstack((a, b)))\n",
    "print(\"==========================================================================================================\")\n",
    "# concatenate函数也是一样的效果\n",
    "print(concatenate((a, b), axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2]\n [ 3  4  5]\n [ 6  7  8]\n [ 0  2  4]\n [ 6  8 10]\n [12 14 16]]\n---------------------------------\n[[ 0  1  2]\n [ 3  4  5]\n [ 6  7  8]\n [ 0  2  4]\n [ 6  8 10]\n [12 14 16]]\n"
     ]
    }
   ],
   "source": [
    "# (2) vstack 垂直组合\n",
    "print(vstack((a, b)))\n",
    "print(\"---------------------------------\")\n",
    "print(concatenate((a,b), axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4]\n [0 1 2 3 4]\n [0 1 2 3 4]\n [0 1 2 3 4]\n [0 1 2 3 4]]\n-----------\n[[1 2 3]\n [1 2 3]\n [1 2 3]]\n"
     ]
    }
   ],
   "source": [
    "# 2.9 数组的分割\n",
    "\n",
    "a = array([arange(5), arange(5), arange(5), arange(5), arange(5)])\n",
    "print(a)\n",
    "print(\"-----------\")\n",
    "print(a[1:-1, 1:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]\n"
     ]
    }
   ],
   "source": [
    "# 2.11 tolist 函数\n",
    "print(a.tolist())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2.0
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}